<template>
  <div class="postList">
    <!-- 导航路径 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/postList' }"
        >文章列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 表格 -->
      <el-table border :data="newsList" style="width: 100%">
        <!-- 第一列的序号 -->
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="类型" width="180">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者" width="180">
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[2, 4, 6, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="150"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { postList } from "@/apis/post";
export default {
  data() {
    return {
      newsList: [],
      pageIndex: 1,
      pageSize: 4,
    };
  },
  async mounted() {
    this.getpostList();
    // console.log(this.newsList);
  },

  methods: {
    // 发请求 拿数据
    async getpostList() {
      let res = await postList({
        pageSize: this.pageSize,
        pageIndex: this.pageIndex,
      });
      this.newsList = res.data.data;
    },
    // 分页
    handleSizeChange(val) {
      console.log(val);

      this.pageSize = val;

      this.getpostList();
    },
    handleCurrentChange(val) {
      console.log(val);

      this.pageIndex = val;
      this.getpostList();
    },

    // 点击编辑时触发
    handleEdit(index, row) {
      // console.log(index, row);
    },
    // 点击删除时触发
    handleDelete(index, row) {
      // console.log(index, row);
    },
  },
};
</script>

<style>
</style>